package com.jh.paymentchannel.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import com.jh.paymentchannel.util.MD5;
import com.jh.paymentchannel.util.Util;
import com.jh.paymentchannel.util.ump.exception.VerifyException;
import com.jh.paymentchannel.util.ump.paygate.v40.Mer2Plat_v40;
import com.jh.paymentchannel.util.ump.paygate.v40.Plat2Mer_v40;


@Controller
@EnableAutoConfiguration
public class ELongCallBackService {

	private static final Logger log = LoggerFactory.getLogger(ELongCallBackService.class);
	
	//商户入驻成功
	@Value("${elong.merid}")
	public   String merchantCode;
	
	@Value("${elong.nkey}")
	private  String nkey;
	
	@Value("${elong.url}")
	private  String url;
	
	@Autowired
	Util util;
	private Map getParamNames(HttpServletRequest request) {    
        Map map = new HashMap();    
        Enumeration paramNames = request.getParameterNames();    
        while (paramNames.hasMoreElements()) {    
            String paramName = (String) paramNames.nextElement();    
    
            String[] paramValues = request.getParameterValues(paramName);    
            if (paramValues.length == 1) {    
                String paramValue = paramValues[0];    
                if (paramValue.length() != 0) {    
                    map.put(paramName, paramValue);    
                }    
            }    
        }    
    
        return map;  
    }  
	
	
	public String getParamsMap(HttpServletRequest request){
		
		
	      Map map = request.getParameterMap();  
	        String text = "";  
	        if (map != null) {  
	            Set set = map.entrySet();  
	            Iterator iterator = set.iterator();  
	            while (iterator.hasNext()) {  
	                Entry entry = (Entry) iterator.next();
	                if (entry.getValue() instanceof String[]) {  
	                	log.info("==A==entry的key： " + entry.getKey());  
	                    String key = (String) entry.getKey();  
	                    if (key != null && !"id".equals(key) && key.startsWith("[") && key.endsWith("]")) {  
	                        text = (String) entry.getKey();  
	                        break;  
	                    }  
	                    String[] values = (String[]) entry.getValue();  
	                    for (int i = 0; i < values.length; i++) {  
	                    	log.info("==B==entry的value: " + values[i]);  
	                        key += "="+values[i];  
	                    }  
	                    if (key.startsWith("[") && key.endsWith("]")) {  
	                        text = (String) entry.getKey();  
	                        break;  
	                    }  
	                } else if (entry.getValue() instanceof String) {  
	                	log.info("==========entry的key： " + entry.getKey());  
	                	log.info("==========entry的value: " + entry.getValue());  
	                }  
	            }  
	        }  
	        return text;  
		
		
		
	}
	
	
	@RequestMapping(method=RequestMethod.GET,value="/v1.0/paymentchannel/topup/elong/notify_call")
	public @ResponseBody void notifycall(HttpServletRequest request, HttpServletResponse response) throws IOException, VerifyException{
		   
		   request.setCharacterEncoding("utf-8");
		   response.setCharacterEncoding("utf-8");
		   response.setContentType("text/html;charset=utf-8");
		   
		   log.info("进入亿龙扫码回调接口啦！！！");
		   Map resData = new HashMap();
		   resData=getParamNames(request);
		   log.debug("艺龙支付回调数据/v1.0/paymentchannel/topup/elong/notify_call:"+resData);
	       String mer_id = request.getParameter("customerid");
	       String resString="no";
	       if(!"".equals(mer_id) && null != mer_id)
	        {
	            log.debug("艺龙支付回调数据");
				//验签,不抛异常表示验签成功
				log.info("艺龙支付回调数据=============== "+resData);
				String signData="customerid="+merchantCode+"&sdpayno="+resData.get("sdpayno")+"&sdcustomno="+resData.get("sdcustomno")+"&ordermoney="+resData.get("ordermoney")+"&key="+nkey;
            String sign= MD5.MD5Encode(signData);
				
				if( resData.get("state")!=null&&resData.get("state").equals("1")&&sign.equalsIgnoreCase(resData.get("sign").toString())){
					resString="ok";
				    //商户可加入自己的处理逻辑
				    /**更新订单状态*/
					/**调用下单，需要得到用户的订单信息*/
					RestTemplate restTemplate=new RestTemplate();
					
					URI uri = util.getServiceUrl("transactionclear", "error url request!");
					String url = uri.toString() + "/v1.0/transactionclear/payment/update";
					
					/**根据的用户手机号码查询用户的基本信息*/
					MultiValueMap<String, String> requestEntity  = new LinkedMultiValueMap<String, String>();
					requestEntity.add("status", "1");
					requestEntity.add("order_code",   resData.get("sdcustomno").toString().trim());
					requestEntity.add("third_code",   resData.get("sdpayno").toString().trim());
					String result = restTemplate.postForObject(url, requestEntity, String.class);
					
					/**判断是否有外放的通道的处理， 如果有那么继续回调外放哦*/
					uri = util.getServiceUrl("transactionclear", "error url request!");
					url = uri.toString() + "/v1.0/transactionclear/payment/query/ordercode";
					
					
					requestEntity  = new LinkedMultiValueMap<String, String>();
					requestEntity.add("order_code",   resData.get("sdcustomno").toString().trim());
					result = restTemplate.postForObject(url, requestEntity, String.class);
					
					JSONObject jsonObject =  JSONObject.fromObject(result);
					JSONObject resultObj  =  jsonObject.getJSONObject("result");
					String outMerOrdercode  =  resultObj.getString("outMerOrdercode");
					String orderdesc        =  resultObj.getString("desc");
					String phone            =  resultObj.getString("phone");
					String  tranamount      =  resultObj.getString("amount");
					String channelTag       =  resultObj.getString("channelTag");
					String notifyURL        =  resultObj.getString("outNotifyUrl");
					if(outMerOrdercode != null && !outMerOrdercode.equalsIgnoreCase("")){
						uri = util.getServiceUrl("channel", "error url request!");
						url = uri.toString() + "/v1.0/channel/callback/yilian/notify_call";
						requestEntity  = new LinkedMultiValueMap<String, String>();
						requestEntity.add("merchant_no",  phone);
						requestEntity.add("amount",  tranamount);
						requestEntity.add("channel_tag",  channelTag);
						requestEntity.add("order_desc",  URLEncoder.encode(orderdesc, "UTF-8"));
						requestEntity.add("order_code",  outMerOrdercode);
						requestEntity.add("sys_order",  resData.get("sdcustomno").toString().trim());
						requestEntity.add("notify_url", URLEncoder.encode(notifyURL, "UTF-8"));
						result = restTemplate.postForObject(url, requestEntity, String.class);
					}
					
				}else if(resData.get("state")!=null&&resData.get("state").equals("2")&&sign.equalsIgnoreCase(resData.get("sign").toString())){
					resString="ok";
				//商户可加入自己的处理逻辑
				/**更新订单状态*/
				/**调用下单，需要得到用户的订单信息*/
				RestTemplate restTemplate=new RestTemplate();
				
				URI uri = util.getServiceUrl("transactionclear", "error url request!");
				String url = uri.toString() + "/v1.0/transactionclear/payment/update";
				
				/**根据的用户手机号码查询用户的基本信息*/
				MultiValueMap<String, String> requestEntity  = new LinkedMultiValueMap<String, String>();
				requestEntity.add("status", "2");
				requestEntity.add("order_code",   resData.get("sdcustomno").toString().trim());
				requestEntity.add("third_code",   resData.get("sdpayno").toString().trim());
				String result = restTemplate.postForObject(url, requestEntity, String.class);
				
				/**判断是否有外放的通道的处理， 如果有那么继续回调外放哦*/
				uri = util.getServiceUrl("transactionclear", "error url request!");
				url = uri.toString() + "/v1.0/transactionclear/payment/query/ordercode";
				
				
				requestEntity  = new LinkedMultiValueMap<String, String>();
				requestEntity.add("order_code",   resData.get("sdcustomno").toString().trim());
				result = restTemplate.postForObject(url, requestEntity, String.class);
				
				JSONObject jsonObject =  JSONObject.fromObject(result);
				JSONObject resultObj  =  jsonObject.getJSONObject("result");
				String outMerOrdercode  =  resultObj.getString("outMerOrdercode");
				String orderdesc        =  resultObj.getString("desc");
				String phone            =  resultObj.getString("phone");
				String  tranamount      =  resultObj.getString("amount");
				String channelTag       =  resultObj.getString("channelTag");
				String notifyURL        =  resultObj.getString("outNotifyUrl");
				if(outMerOrdercode != null && !outMerOrdercode.equalsIgnoreCase("")){
					uri = util.getServiceUrl("channel", "error url request!");
					url = uri.toString() + "/v1.0/channel/callback/yilian/notify_call";
					requestEntity  = new LinkedMultiValueMap<String, String>();
					requestEntity.add("merchant_no",  phone);
					requestEntity.add("amount",  tranamount);
					requestEntity.add("channel_tag",  channelTag);
					requestEntity.add("order_desc",  URLEncoder.encode(orderdesc, "UTF-8"));
					requestEntity.add("order_code",  outMerOrdercode);
					requestEntity.add("sys_order",   resData.get("sdcustomno").toString().trim());
					requestEntity.add("notify_url", URLEncoder.encode(notifyURL, "UTF-8"));
					result = restTemplate.postForObject(url, requestEntity, String.class);
				}
					
					
				}
	        }
	       PrintWriter write = response.getWriter();
	       write.print(resString);
	       write.flush();
	       
	}
		
	

}
